﻿using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using Cms.Utilities;
using NHibernate.Tool.hbm2ddl;

namespace Cms.Data.ORM
{
    public class Installer
    {
        private IPermissionRepository _permissionRepository;

        public Installer()
        {
            _permissionRepository = new PermissionRepository();
        }

        public Installer(IPermissionRepository permissionRepository)
        {
            _permissionRepository = permissionRepository;
        }

        public void CreateDatabase()
        {
            Logging.LogInfo("Enter CreateDatabase");
            var schemaExport = new SchemaExport(NHibernateSessionFactory.Configuration);
            Logging.LogInfo("schemaExport created");
            schemaExport.SetDelimiter(";");
            schemaExport.Execute(true, true, true); // Drop
            Logging.LogInfo("drop successful");
            schemaExport.Execute(true, true, false); // Create
            Logging.LogInfo("create successful");
            var adminRepository = new AdministratorRepository();
            var group = new AdministratorGroup
                            {
                                DefaultPageAccess = DefaultAccess.Allow,
                                Name = "Administrators"
                            };
            adminRepository.SaveGroup(group);
            Logging.LogInfo("admingroup created");
            var admin = new Administrator
                            {
                                Group = group,
                                Username = "admin",
                                Password = "admin",
                                DefaultSystemLocale = "en-US"
                            };
            group.Administrators.Add(admin);
            adminRepository.SaveAdministrator(admin);
            Logging.LogInfo("admin created");
        }

        public void CreateFieldTypes()
        {
            var systemRepository = new SystemRepository();
            FieldType username = new FieldType { Id = 1, Name = "Username", IsValueset = false, SortIndex = 1 };
            systemRepository.SaveFieldType(username);
            FieldType password = new FieldType { Id = 2, Name = "Password", IsValueset = false, SortIndex = 2 };
            systemRepository.SaveFieldType(password);
            FieldType email = new FieldType { Id = 3, Name = "Email", IsValueset = false, SortIndex = 3 };
            systemRepository.SaveFieldType(email);
            FieldType textSingleRow = new FieldType { Id = 4, Name = "TextSingleRow", IsValueset = false, SortIndex = 4 };
            systemRepository.SaveFieldType(textSingleRow);
            FieldType textMultipleRows = new FieldType { Id = 5, Name = "TextMultipleRows", IsValueset = false, SortIndex = 5 };
            systemRepository.SaveFieldType(textMultipleRows);
        }

        public void CreatePermissions()
        {
            AddPermission(1, "ConfigureFirstSite", 1, null);
            var explorer = AddPermission(2, "Explorer", 2, null);
            AddPermission(3, "ExplorerAddFolder", 1, explorer);
            AddPermission(21, "EditFolder", 2, explorer);
            AddPermission(22, "EditPage", 3, explorer);
            AddPermission(23, "EditApplicationPage", 4, explorer);
            var sites = AddPermission(4, "Sites", 3, null);
            AddPermission(30, "EditSite", 1, sites);
            AddPermission(5, "PublishContent", 4, null);
            var userGroups = AddPermission(6, "UserGroups", 5, null);
            AddPermission(14, "EditUserGroup", 1, userGroups);
            var administrators = AddPermission(7, "Administrators", 6, null);
            AddPermission(18, "EditAdministrator", 1, administrators);
            var administratorGroups = AddPermission(8, "AdministratorGroups", 7, null);
            AddPermission(17, "EditAdministratorGroup", 1, administratorGroups);
            var plugins = AddPermission(9, "Plugins", 8, null);
            var pluginTemplates = AddPermission(10, "PluginTemplates", 1, plugins);
            AddPermission(11, "EditPluginTemplate", 1, pluginTemplates);
            AddPermission(12, "EditPluginTemplateLiterals", 2, pluginTemplates);
            AddPermission(13, "Comments", 9, null);
            var categories = AddPermission(15, "Categories", 10, null);
            AddPermission(16, "EditCategory", 1, categories);
            var customAdminPages = AddPermission(19, "CustomAdminPages", 11, null);
            AddPermission(20, "EditCustomAdminPage", 1, customAdminPages);
            var stylesheets = AddPermission(24, "Stylesheets", 12, null);
            AddPermission(25, "EditStylesheet", 1, stylesheets);
            AddPermission(26, "Update", 13, null);
            var locales = AddPermission(27, "Locales", 14, null);
            AddPermission(28, "EditLocale", 1, locales);
            AddPermission(29, "DashboardGadgets", 15, null);
            var masterTemplates = AddPermission(31, "MasterTemplates", 16, null);
            AddPermission(32, "EditMasterTemplate", 1, masterTemplates);
            var detailTemplates = AddPermission(33, "DetailTemplates", 17, null);
            AddPermission(34, "EditDetailTemplate", 1, detailTemplates);
            var applications = AddPermission(35, "Applications", 18, null);
            AddPermission(36, "EditApplication", 1, applications);
            AddPermission(37, "ApplicationPages", 2, applications);
            var searchContent = AddPermission(38, "SearchContent", 19, null);
            AddPermission(39, "EditContent", 20, null);
            AddPermission(40, "EditCustomDashboardGadget", 21, null);
            var menus = AddPermission(41, "Menus", 22, null);
            var editMenu = AddPermission(42, "EditMenu", 1, menus);
            AddPermission(43, "EditMenuItem", 1, editMenu);
            var media = AddPermission(44, "Media", 23, null);
            AddPermission(45, "Upload", 1, media);
            AddPermission(46, "EditMediaFolder", 2, media);
            AddPermission(47, "EditMediaFile", 3, media);
            AddPermission(48, "EditContentPreview", 1, searchContent);
            var users = AddPermission(49, "Users", 24, null);
            AddPermission(50, "EditUser", 1, users);
            AddPermission(51, "AddUser", 2, users);
            var rssFeeds = AddPermission(52, "RssFeeds", 25, null);
            AddPermission(53, "AddRssFeed", 1, rssFeeds);
            AddPermission(54, "EditRssFeed", 2, rssFeeds);
            AddPermission(55, "DeleteRssFeed", 3, rssFeeds);
            AddPermission(56, "CustomizeEditor", 26, null);

            // Next main sort index = 27
        }

        private Permission AddPermission(int id, string name, int sortIndex, Permission parent)
        {
            var permission = new Permission { ID = id, Name = name, SortIndex = sortIndex, Parent = parent };
            _permissionRepository.SavePermission(permission);
            return permission;
        }
    }
}